package com.sc.VenusQueryBeta.model.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hengjun.kingstar.sdk.utils.LogUtil;
import com.hengjun.kingstar.sdk.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 20/11/2018.
 *
 */

public class DataBaseDao {

    public Context context;
    public DatabaseHelper databasehelper;
    public SQLiteDatabase db;


    public DataBaseDao(Context context, String name, int version) {
        this.context = context;
        databasehelper = DatabaseHelper.getInstance(context, name, version);
    }

    //插入数据
    public void insertInto(String linkmanID, String sendClientIndex, String receiveClientIndex, String messageContent, String sendTime
            , String isRead, String sendAreaCode, String receiveAreaCode, String sendId, String receiveId, String messageIndex, String voicePath, String voiceTime, String isListened
            , String messageType, String sendStatus, String resourcePath) {
        db = databasehelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("linkmanID", linkmanID);
        contentValues.put("sendClientIndex", sendClientIndex);
        contentValues.put("receiveClientIndex", receiveClientIndex);
        contentValues.put("messageContent", messageContent);
        contentValues.put("sendTime", sendTime);
        contentValues.put("isRead", isRead);
        contentValues.put("sendAreaCode", sendAreaCode);
        contentValues.put("receiveAreaCode", receiveAreaCode);
        contentValues.put("sendId", sendId);
        contentValues.put("receiveId", receiveId);
        contentValues.put("messageIndex", messageIndex);
        contentValues.put("voicePath", voicePath);
        contentValues.put("voiceTime", voiceTime);
        contentValues.put("isListened", isListened);
        contentValues.put("messageType", messageType);
        contentValues.put("sendStatus", sendStatus);
        contentValues.put("resourcePath", resourcePath);
//        db.execSQL("INSERT INTO messages(linkmanID,sendClientIndex,receiveClientIndex,messageContent,sendTime," +
//                "isRead,sendAreaCode,receiveAreaCode,sendId,receiveId,messageIndex,voicePath,voiceTime,isListened," +
//                "messageType,sendStatus,resourcePath) VALUES(linkmanID,sendClientIndex,receiveClientIndex)");
        db.insert("messages", null, contentValues);

//        db.setTransactionSuccessful();
    }

    //删除数据
    public void deleteMsg(String sendId) {
        db = databasehelper.getWritableDatabase();
//        db.beginTransaction();
        // delete from Orders where messageIndex = ?
        int messages = db.delete("messages", "sendId = ?", new String[]{sendId});
        int messages1 = db.delete("reads", "sendId = ?", new String[]{sendId});
        LogUtil.e("messages=" + messages);
//        db.setTransactionSuccessful();

    }

    //删除数据
    public void deleteReadMsg(String sendId) {
        db = databasehelper.getWritableDatabase();
        int messages1 = db.delete("reads", "sendId = ?", new String[]{sendId});

    }

    public List<MsgListBean> searchMsg() {
        db = databasehelper.getReadableDatabase();
        List<com.sc.VenusQueryBeta.model.sqlite.MsgListBean> list = new ArrayList<>();
        list.clear();
        Cursor cursor = db.query("messages", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
//            do {
//                MsgListBean.MessageListBean msgListBean = new MsgListBean().getMessageList().get(0);
            MsgListBean msgListBean = new MsgListBean();
            // 遍历Cursor对象，取出数据并保存
            String linkmanID = cursor.getString(cursor.getColumnIndex("linkmanID"));
            String sendClientIndex = cursor.getString(cursor.getColumnIndex("sendClientIndex"));
            String receiveClientIndex = cursor.getString(cursor.getColumnIndex("receiveClientIndex"));
            String messageContent = cursor.getString(cursor.getColumnIndex("messageContent"));
            String sendTime = cursor.getString(cursor.getColumnIndex("sendTime"));
            String isRead = cursor.getString(cursor.getColumnIndex("isRead"));
            String sendAreaCode = cursor.getString(cursor.getColumnIndex("sendAreaCode"));
            String receiveAreaCode = cursor.getString(cursor.getColumnIndex("receiveAreaCode"));
            String sendId = cursor.getString(cursor.getColumnIndex("sendId"));
            LogUtil.e("isread==" + isRead);
            String receiveId = cursor.getString(cursor.getColumnIndex("receiveId"));
            String messageIndex = cursor.getString(cursor.getColumnIndex("messageIndex"));
            String voicePath = cursor.getString(cursor.getColumnIndex("voicePath"));
            String voiceTime = cursor.getString(cursor.getColumnIndex("voiceTime"));
            String isListened = cursor.getString(cursor.getColumnIndex("isListened"));
            String messageType = cursor.getString(cursor.getColumnIndex("messageType"));
            String sendStatus = cursor.getString(cursor.getColumnIndex("sendStatus"));
            String resourcePath = cursor.getString(cursor.getColumnIndex("resourcePath"));

            msgListBean.setSendId(sendId);
            msgListBean.setSendTime(sendTime);
            msgListBean.setReceiveAreaCode(receiveAreaCode);
            msgListBean.setSendAreaCode(sendAreaCode);
            msgListBean.setMessageContent(messageContent);
            msgListBean.setReceiveClientIndex(Integer.parseInt(receiveClientIndex));
            msgListBean.setSendClientIndex(Integer.parseInt(sendClientIndex));
            msgListBean.setIsRead(isRead);
            msgListBean.setReceiveId(receiveId);
            msgListBean.setMessageIndex(Integer.parseInt(messageIndex));
            msgListBean.setVoicePath(voicePath);
            msgListBean.setVoiceTime(Integer.parseInt(voiceTime));
//                msgListBean.setIsListened(isListened);
            msgListBean.setMessageType(messageType);

            list.add(msgListBean);

//            }while (cursor.moveToNext());

        }
        cursor.close();
        LogUtil.e("list size = " + list.size());
        return list;
    }

    public List<MsgListBean> searchReadMsg() {
        db = databasehelper.getReadableDatabase();
        List<com.sc.VenusQueryBeta.model.sqlite.MsgListBean> list = new ArrayList<>();
        list.clear();
        Cursor cursor = db.query("reads", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
//            do {
//                MsgListBean.MessageListBean msgListBean = new MsgListBean().getMessageList().get(0);
            MsgListBean msgListBean = new MsgListBean();
            // 遍历Cursor对象，取出数据并保存
            String linkmanID = cursor.getString(cursor.getColumnIndex("linkmanID"));
            String sendClientIndex = cursor.getString(cursor.getColumnIndex("sendClientIndex"));
            String receiveClientIndex = cursor.getString(cursor.getColumnIndex("receiveClientIndex"));
            String messageContent = cursor.getString(cursor.getColumnIndex("messageContent"));
            String sendTime = cursor.getString(cursor.getColumnIndex("sendTime"));
            String isRead = cursor.getString(cursor.getColumnIndex("isRead"));
            String sendAreaCode = cursor.getString(cursor.getColumnIndex("sendAreaCode"));
            String receiveAreaCode = cursor.getString(cursor.getColumnIndex("receiveAreaCode"));
            String sendId = cursor.getString(cursor.getColumnIndex("sendId"));
            LogUtil.e("isread==" + isRead);
            String receiveId = cursor.getString(cursor.getColumnIndex("receiveId"));
            String messageIndex = cursor.getString(cursor.getColumnIndex("messageIndex"));
            String voicePath = cursor.getString(cursor.getColumnIndex("voicePath"));
            String voiceTime = cursor.getString(cursor.getColumnIndex("voiceTime"));
            String isListened = cursor.getString(cursor.getColumnIndex("isListened"));
            String messageType = cursor.getString(cursor.getColumnIndex("messageType"));
            String sendStatus = cursor.getString(cursor.getColumnIndex("sendStatus"));
            String resourcePath = cursor.getString(cursor.getColumnIndex("resourcePath"));

            msgListBean.setSendId(sendId);
            msgListBean.setSendTime(sendTime);
            msgListBean.setReceiveAreaCode(receiveAreaCode);
            msgListBean.setSendAreaCode(sendAreaCode);
            msgListBean.setMessageContent(messageContent);
            msgListBean.setReceiveClientIndex(Integer.parseInt(receiveClientIndex));
            msgListBean.setSendClientIndex(Integer.parseInt(sendClientIndex));
            msgListBean.setIsRead(isRead);
            msgListBean.setReceiveId(receiveId);
            msgListBean.setMessageIndex(Integer.parseInt(messageIndex));
            msgListBean.setVoicePath(voicePath);
            msgListBean.setVoiceTime(Integer.parseInt(voiceTime));
//                msgListBean.setIsListened(isListened);
            msgListBean.setMessageType(messageType);

            list.add(msgListBean);

//            }while (cursor.moveToNext());

        }
        cursor.close();
        LogUtil.e("list size = " + list.size());
        return list;
    }

    /**
     * 查询详情的
     * @param sid
     * @return
     */
    public List<MsgListBean> searchMsg1(String sid) {
        db = databasehelper.getReadableDatabase();
        List<com.sc.VenusQueryBeta.model.sqlite.MsgListBean> list = new ArrayList<>();
        list.clear();
        Cursor cursor = db.query("messages", null, "sendId = ?", new String[]{sid}, null, null, null);
        while (cursor.moveToNext()) {
//            do {
//                MsgListBean.MessageListBean msgListBean = new MsgListBean().getMessageList().get(0);
            MsgListBean msgListBean = new MsgListBean();
            // 遍历Cursor对象，取出数据并保存
            String linkmanID = cursor.getString(cursor.getColumnIndex("linkmanID"));
            String sendClientIndex = cursor.getString(cursor.getColumnIndex("sendClientIndex"));
            String receiveClientIndex = cursor.getString(cursor.getColumnIndex("receiveClientIndex"));
            String messageContent = cursor.getString(cursor.getColumnIndex("messageContent"));
            String sendTime = cursor.getString(cursor.getColumnIndex("sendTime"));
            String isRead = cursor.getString(cursor.getColumnIndex("isRead"));
            String sendAreaCode = cursor.getString(cursor.getColumnIndex("sendAreaCode"));
            String receiveAreaCode = cursor.getString(cursor.getColumnIndex("receiveAreaCode"));
            String sendId = cursor.getString(cursor.getColumnIndex("sendId"));
            LogUtil.e("sendasid==" + sendId);
            String receiveId = cursor.getString(cursor.getColumnIndex("receiveId"));
            String messageIndex = cursor.getString(cursor.getColumnIndex("messageIndex"));
            String voicePath = cursor.getString(cursor.getColumnIndex("voicePath"));
            String voiceTime = cursor.getString(cursor.getColumnIndex("voiceTime"));
            String isListened = cursor.getString(cursor.getColumnIndex("isListened"));
            String messageType = cursor.getString(cursor.getColumnIndex("messageType"));
            String sendStatus = cursor.getString(cursor.getColumnIndex("sendStatus"));
            String resourcePath = cursor.getString(cursor.getColumnIndex("resourcePath"));

            msgListBean.setSendId(sendId);
            msgListBean.setSendTime(sendTime);
            msgListBean.setReceiveAreaCode(receiveAreaCode);
            msgListBean.setSendAreaCode(sendAreaCode);
            msgListBean.setMessageContent(messageContent);
            msgListBean.setReceiveClientIndex(Integer.parseInt(receiveClientIndex));
            msgListBean.setSendClientIndex(Integer.parseInt(sendClientIndex));
            msgListBean.setIsRead(isRead);
            msgListBean.setReceiveId(receiveId);
            msgListBean.setMessageIndex(Integer.parseInt(messageIndex));
            msgListBean.setVoicePath(voicePath);
            msgListBean.setVoiceTime(Integer.parseInt(voiceTime));
//                msgListBean.setIsListened(isListened);
            msgListBean.setMessageType(messageType);

            list.add(msgListBean);

//            }while (cursor.moveToNext());

        }
        cursor.close();
        LogUtil.e("list size = " + list.size());
        return list;
    }

    /**
     * 查询消息是否已读
     */
    public boolean searchMessageIsRead(String sendId) {
        if (db != null && sendId != null) {
            Cursor cursor = db.query("reads", null, "sendId = ?", new String[]{sendId}, null, null, null);
//            while (cursor.moveToLast()) {
            cursor.moveToLast();
            String isRead = cursor.getString(cursor.getColumnIndex("isRead"));
            LogUtil.e("查询是否已读：" + isRead);
            if (isRead.equals("1")) {
                return true;
            } else {
                LogUtil.e("有唯独的消息");
            }
//            }
            cursor.close();
        } else {
            LogUtil.e("nullllllllllllll");
        }
        return false;
    }

    public boolean searchMessageIsReadAll(String sendId) {
        if (db != null && sendId != null) {
            Cursor cursor = db.query("reads", null, "sendId = ?", new String[]{sendId}, null, null, null);
            while (cursor.moveToLast()) {
//            cursor.moveToLast();
                String isRead = cursor.getString(cursor.getColumnIndex("isRead"));
                LogUtil.e("查询是否已读：" + isRead);
                if (isRead.equals("1")) {
                    return true;
                } else {
                    LogUtil.e("有唯独的消息");
                }
            }
            cursor.close();
        } else {
            LogUtil.e("nullllllllllllll");
        }
        return false;
    }

    /**
     * 设置消息已读
     */
    public void setMessageIsRead(String sendId) {
        if (db != null && sendId != null) {
            LogUtil.e("设置消息已读:" + sendId);
            String sql = "update reads set isRead=? where sendId=?";
            Object bindArgs[] = new Object[]{1, sendId};
            db.execSQL(sql, bindArgs);
        }
    }

    /**
     * 查询是否存在read数据
     */
    public  boolean searchMessage(String sendId){
        if(db!=null&&sendId!=null){
            Cursor cursor = db.rawQuery("select * from reads where sendId=? ",new String[]{sendId});
            while (cursor.moveToNext()){
                cursor.close();
                return true;
            }
        }
        return false;
    }

    //插入read数据
    public void insertRead(String linkmanID, String sendClientIndex, String receiveClientIndex, String messageContent, String sendTime
            , String isRead, String sendAreaCode, String receiveAreaCode, String sendId, String receiveId, String messageIndex, String voicePath, String voiceTime, String isListened
            , String messageType, String sendStatus, String resourcePath) {
        if (searchMessage(sendId)) {//有这条记录
//            ToastUtils.showToast("有这条记录");
//            LogUtil.e("有这条记录");
//            if (searchMessageIsRead(sendId)) {//已读了
                LogUtil.e("这条记录读了");
                String sql = "update reads set isRead=? where sendId=?";
                Object bindArgs[] = new Object[]{0, sendId};
                db.execSQL(sql, bindArgs);
//            } else {//未读
//                LogUtil.e("这条记录未读");
//            }
        } else {
            db = databasehelper.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("linkmanID", linkmanID);
            contentValues.put("sendClientIndex", sendClientIndex);
            contentValues.put("receiveClientIndex", receiveClientIndex);
            contentValues.put("messageContent", messageContent);
            contentValues.put("sendTime", sendTime);
            contentValues.put("isRead", isRead);
            contentValues.put("sendAreaCode", sendAreaCode);
            contentValues.put("receiveAreaCode", receiveAreaCode);
            contentValues.put("sendId", sendId);
            contentValues.put("receiveId", receiveId);
            contentValues.put("messageIndex", messageIndex);
            contentValues.put("voicePath", voicePath);
            contentValues.put("voiceTime", voiceTime);
            contentValues.put("isListened", isListened);
            contentValues.put("messageType", messageType);
            contentValues.put("sendStatus", sendStatus);
            contentValues.put("resourcePath", resourcePath);
//        db.execSQL("INSERT INTO messages(linkmanID,sendClientIndex,receiveClientIndex,messageContent,sendTime," +
//                "isRead,sendAreaCode,receiveAreaCode,sendId,receiveId,messageIndex,voicePath,voiceTime,isListened," +
//                "messageType,sendStatus,resourcePath) VALUES(linkmanID,sendClientIndex,receiveClientIndex)");
            db.insert("reads", null, contentValues);
        }

//        db.setTransactionSuccessful();
    }

    /**
     * 查询所有的read
     * @return
     */
    public List<MsgListBean> searchAllRead() {
        db = databasehelper.getReadableDatabase();
        List<com.sc.VenusQueryBeta.model.sqlite.MsgListBean> list = new ArrayList<>();
        list.clear();
        Cursor cursor = db.query("reads", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
//            do {
//                MsgListBean.MessageListBean msgListBean = new MsgListBean().getMessageList().get(0);
            MsgListBean msgListBean = new MsgListBean();
            // 遍历Cursor对象，取出数据并保存
            String linkmanID = cursor.getString(cursor.getColumnIndex("linkmanID"));
            String sendClientIndex = cursor.getString(cursor.getColumnIndex("sendClientIndex"));
            String receiveClientIndex = cursor.getString(cursor.getColumnIndex("receiveClientIndex"));
            String messageContent = cursor.getString(cursor.getColumnIndex("messageContent"));
            String sendTime = cursor.getString(cursor.getColumnIndex("sendTime"));
            String isRead = cursor.getString(cursor.getColumnIndex("isRead"));
            String sendAreaCode = cursor.getString(cursor.getColumnIndex("sendAreaCode"));
            String receiveAreaCode = cursor.getString(cursor.getColumnIndex("receiveAreaCode"));
            String sendId = cursor.getString(cursor.getColumnIndex("sendId"));
            LogUtil.e("isRead111==" + isRead+"sendid=:"+sendId);
            String receiveId = cursor.getString(cursor.getColumnIndex("receiveId"));
            String messageIndex = cursor.getString(cursor.getColumnIndex("messageIndex"));
            String voicePath = cursor.getString(cursor.getColumnIndex("voicePath"));
            String voiceTime = cursor.getString(cursor.getColumnIndex("voiceTime"));
            String isListened = cursor.getString(cursor.getColumnIndex("isListened"));
            String messageType = cursor.getString(cursor.getColumnIndex("messageType"));
            String sendStatus = cursor.getString(cursor.getColumnIndex("sendStatus"));
            String resourcePath = cursor.getString(cursor.getColumnIndex("resourcePath"));

            msgListBean.setSendId(sendId);
            msgListBean.setSendTime(sendTime);
            msgListBean.setReceiveAreaCode(receiveAreaCode);
            msgListBean.setSendAreaCode(sendAreaCode);
            msgListBean.setMessageContent(messageContent);
            msgListBean.setReceiveClientIndex(Integer.parseInt(receiveClientIndex));
            msgListBean.setSendClientIndex(Integer.parseInt(sendClientIndex));
            msgListBean.setIsRead(isRead);
            msgListBean.setReceiveId(receiveId);
            msgListBean.setMessageIndex(Integer.parseInt(messageIndex));
            msgListBean.setVoicePath(voicePath);
            msgListBean.setVoiceTime(Integer.parseInt(voiceTime));
//                msgListBean.setIsListened(isListened);
            msgListBean.setMessageType(messageType);

            list.add(msgListBean);

//            }while (cursor.moveToNext());

        }
        cursor.close();
        LogUtil.e("list size = " + list.size());
        return list;
    }
}
